package com.example.dell.fastshopping.network.http;

import android.content.Context;
import android.widget.Toast;

import com.example.dell.fastshopping.utils.KLog;
import com.example.dell.fastshopping.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

import java.io.IOException;

import io.reactivex.observers.DisposableObserver;
import okhttp3.ResponseBody;
import retrofit2.HttpException;


/**
 * Created by goldze on 2017/5/10.
 * 该类仅供参考，实际业务Code, 根据需求来定义，
 */
public abstract class BaseSubscriber extends DisposableObserver {
    public abstract void onResult(BaseResponse t);

    private Context context;
    private boolean isNeedCahe;

    public BaseSubscriber(Context context) {
        this.context = context;
    }

    @Override
    public void onError(Throwable e) {
        // KLog.e(e.getMessage());
        // todo error somthing
        if (e instanceof HttpException) {
            HttpException exception = (HttpException) e;
            if (exception.code() == 500) {
                ResponseBody responseBody = exception.response().errorBody();
                if (responseBody != null) {
                    String json = null;//第一次获取就保存下来
                    try {
                        json = responseBody.string();
                        BaseResponse baseResponse = new Gson().fromJson(json, BaseResponse.class);
                        ToastUtils.showShort(baseResponse.getMsg());
                        return;
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }

                }

            }
        }

        if (e instanceof ResponseThrowable) {
            onError((ResponseThrowable) e);
            return;
        } else {
            onError(new ResponseThrowable(e, ExceptionHandle.ERROR.UNKNOWN));
            ToastUtils.showShort("未知错误！");
        }


    }


    @Override
    public void onStart() {
        super.onStart();

        //  Toast.makeText(context, "http is start", Toast.LENGTH_SHORT).show();
        // todo some common as show loadding  and check netWork is NetworkAvailable
        // if  NetworkAvailable no !   must to call onCompleted
        if (!NetworkUtil.isNetworkAvailable(context)) {
            Toast.makeText(context, "无网络，读取缓存数据", Toast.LENGTH_SHORT).show();
            onComplete();
        }

    }

    @Override
    public void onComplete() {

        // Toast.makeText(context, "http is Complete", Toast.LENGTH_SHORT).show();
        // todo some common as  dismiss loadding
    }


    public abstract void onError(ResponseThrowable e);

    @Override
    public void onNext(Object b) {
        BaseResponse baseResponse;
        if (b instanceof LinkedTreeMap) {
            KLog.e("LinkedTreeMap");
            LinkedTreeMap treeMap = (LinkedTreeMap) b;
            if (treeMap.get("data") == null) {
                baseResponse = new BaseResponse(treeMap.get("code") + "", treeMap.get("msg") + "");
            } else {
                baseResponse = (BaseResponse) b;
            }
        } else {
            baseResponse = (BaseResponse) b;
        }

        if (baseResponse.getCode() == 200) {
            onResult(baseResponse);
        } else if (baseResponse.getCode() == 330) {
            ToastUtils.showShort(baseResponse.getMsg());
        } else if (baseResponse.getCode() == 503) {
            KLog.e(baseResponse.getMsg());
        } else {
            ToastUtils.showShort(baseResponse.getMsg());
        }
    }
}
